package com.bigfreakindeals.Activities;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ScheduledExecutorService;

import com.bigfreakindeals.R;
import com.bigfreakindeals.Application.ApplicationEx;
import com.bigfreakindeals.Common.Constant;
import com.bigfreakindeals.Common.Tools;
import com.bigfreakindeals.Models.UniversityDeals;
import com.bigfreakindeals.Protocols.SoapHandlers.ClosestSchoolHandler;
import com.bigfreakindeals.Protocols.SoapHandlers.GetSchoolListHandler;
import com.bigfreakindeals.Protocols.SoapHandlers.GetTodaysDealHandler;

import android.app.Activity;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class GetSchoolAllActivity extends BaseActivity implements OnItemClickListener {

	ListView lv1;
	TextView headertitle;
	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.schoollist);

		lv1 = (ListView) findViewById(R.id.list);
		headertitle = (TextView) findViewById(R.id.header_title);
		headertitle.setText("Select School");
		
		lv1.setOnItemClickListener(this);
		
		this.registerReceiver(getSchooListBroadcastReceiver, new IntentFilter(Constant.EVENTS_GETSCHOOLLIST));
		
		
		System.out.println("getSchoolactitivty ONCREATE");
		
				
		

	}
public void getScreen(){
	if (ApplicationEx.getInstance().getSchoolListCompletedStatus == 0 && ApplicationEx.getInstance().getSchoolListCompletedStatus == 1){
				new Thread() {
			public void run() {
				try {
					sleep(5);
				} catch (Exception e) {
				}
				System.out.println("GetSchoolListHandler ONCREATE");
				
				new GetSchoolListHandler(Tools.getURL(Constant.GET_SCHOOLLIST, null));
			}

		}.start();
	}else if (ApplicationEx.getInstance().getSchoolListCompletedStatus == 3){
		refreshScreen();
	}

}

	public void refreshScreen() {

		((ProgressBar) findViewById(R.id.progress)).setVisibility(View.INVISIBLE);
		
		
		String[] getSchoolListString  = Tools.getStringArray(ApplicationEx.getInstance().schoolListString);
			
		lv1.setAdapter(new ArrayAdapter<String>(this,R.layout.schoollisitem, getSchoolListString));
		lv1.setSelection(0);
	

		
	}
	
	

	@Override
	public void onResume() {
		super.onResume();
		getScreen();
		Log.i("GETSCHOOL", "getSchoollist - OnResume");

	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		this.unregisterReceiver(getSchooListBroadcastReceiver);

	}

	public BroadcastReceiver getSchooListBroadcastReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			
			if (intent.getAction().equals(Constant.EVENTS_GETSCHOOLLIST)) {
				// update UI
				Log.i("GETSCHOOL", "getSchoollist - EVENTS");

				refreshScreen();

			}
		}
	};
	@Override
	public void onPause(){
		super.onPause();
		if (ApplicationEx.getInstance().progressDialog!=null)
			ApplicationEx.getInstance().progressDialog.dismiss();
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		// TODO Auto-generated method stub
			
		ApplicationEx.getInstance().myProfile.setlastSelectedSchool(ApplicationEx.getInstance().schoolListMap.get(lv1.getAdapter().getItem(position)));
		
		UniversityDeals univ = ApplicationEx.getInstance().schoolListMap.get(lv1.getAdapter().getItem(position));
		
		
		// start pulling the latest deals
		try
		{		ApplicationEx.getInstance().progressDialog = ProgressDialog.show(this, "BigFreakinDeals", "Loading "+ univ.getSchoolName() + ". Please wait...");
		}catch (Exception e) {
			// TODO: handle exception
		
		}ApplicationEx.getInstance().controller.current = this;
		
		ApplicationEx.getInstance().controller.getTodaysDeal(true);
		
	}

}
